package util;

import java.io.*;
import java.time.LocalDate;
import java.util.UUID;

/**
 * @classname:FileUtil
 * @description:
 * @author: Mr.Z
 * @date: 2020/12/29 23:32
 * @version: 0.1
 * @since:1.8
 */


public class FileUtil {

    private FileUtil() {
    }

    private static final String TARGET_DIRECTORY = "upload/user/";

    public static String fileUpload(String sourceFilePath) {

        //upload/user/2020-12-29/a.jpg  文件读写

        String curDateStr = LocalDate.now().toString();
        //判断子级目录是否是存在的 upload/user/2020-12-29/
        File childDirectory = new File(TARGET_DIRECTORY, curDateStr);
        if (!childDirectory.exists()) {
            childDirectory.mkdirs();
        }

//        String targetFilePath = "upload/user/"+curDateStr;
        String fileName = sourceFilePath.substring(sourceFilePath.lastIndexOf(File.separatorChar) + 1);
        String uuidStr = UUID.randomUUID().toString().replaceAll("-", "");
        File childFile = new File(childDirectory, uuidStr + "_" + fileName);

        try (
                BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(sourceFilePath));
                BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(childFile))
        ) {
            byte[] bytes = new byte[1024];
            int len = 0;
            while ((len = inputStream.read(bytes)) != -1) {
                outputStream.write(bytes, 0, len);
            }

            System.out.println("文件上传成功:" + childFile.getPath());
            return childFile.getPath();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

}

//public class FileUtil {
//    private FileUtil() {
//    }
//
//    private static final String TARGET_DIRECTORY = "upload/user/";
//
//    public static String fileUpload(String sourceFilePath) {
//
//        String data = LocalDate.now().toString();
//
//        File childDirectory = new File(TARGET_DIRECTORY, data);
//
//        if (!childDirectory.exists()) {
//            childDirectory.mkdirs();
//        }
//        String pictureName = sourceFilePath.substring(sourceFilePath.lastIndexOf(File.separatorChar) + 1);
//        String uuidStr = UUID.randomUUID().toString().replaceAll("-", "");
//        String fileName = uuidStr + "-" + pictureName;
//        File targetFilePath = new File(childDirectory, fileName);
//
//        try (
//                BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(sourceFilePath));
//                BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(targetFilePath));
//        ) {
//            byte[] bytes = new byte[1024];
//            int len = 0;
//            while ((len = bufferedInputStream.read(bytes)) != -1) {
//                bufferedOutputStream.write(bytes, 0, len);
//            }
//            System.out.println("文件上传成功 " + targetFilePath.getPath());
//            return targetFilePath.getPath();
//        } catch (FileNotFoundException e) {
//            e.printStackTrace();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return null;
//    }
//}
